package com.wangzhen.spring.ioc.dependcy.injection;

import java.util.Collection;
import java.util.Map;

import com.wangzhen.spring.ioc.dependcy.respository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wangzhen.spring.ioc.dependcy.annotation.MyBean;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.core.env.Environment;

/**
 * 依赖来源示例：
 * 1、spring内建bean
 * 2、用户自定义bean
 *
 * @author dell
 * @since 2020/9/18 12:37
 */
public class DependcySourceDemo {

    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependcy-injection-context.xml");
        // 依赖来源1：自定义的bean(通过XML配置的UserRespository)
        UserRepository userRepository = factory.getBean(UserRepository.class);
        Collection<User> users = userRepository.getUsers();
        System.out.println("userRepository.getUsers == "+ users);
        System.out.println("userRepository.getBeanFactory == "+userRepository.getBeanFactory());

        // 在dependcy-injection-context.xml 中按照autowriing=byType注入一个private BeanFactory beanFactory;
        // 打印比较看这两个对象是否相等，结果是不相等的
        System.out.println(factory == userRepository.getBeanFactory());

        // 在自动注入一个  private ObjectFactory<ApplicationContext> objectFactory;
        // 比较获取出来的对象是否和上面实例化的factory是否相等
        System.out.println("userRepository.getObjectFactory().getObject() == factory 的结果："+ (userRepository.getObjectFactory().getObject() == factory)  );

//        System.out.println(factory.getBean(BeanFactory.class));

        // 依赖来源3：由容器创建的bean
        Environment environment = factory.getBean(Environment.class);
        System.out.println("spring 内建Environment Bean："+environment);

        whoIsIocContainer(userRepository, factory);
    }

    private static void whoIsIocContainer(UserRepository userRepository, BeanFactory beanFactory) {
        // 这个表达式为什么不成立
        System.out.println(userRepository.getBeanFactory() == beanFactory);


    }


}
